<?php defined('SYSPATH') or die('No direct script access.');
/**
* Array List
*/
class Helper_ArrayList {
	
	static function &factory() {
	   $o = new Helper_ArrayList();
	  return $o;	
	}
	
  // Create List of Hyperlink Target for Hyperlink Target Selection
	public function hyperlink_target()
	{
		$hyperlinktarget['_blank'] = '_blank';
		$hyperlinktarget['_self'] = '_self';
		
		return $hyperlinktarget;
	}
	
  // Create List of Country and ID as Array for Country Selection
	public function country_list()
	{
		$countries = DB::select('id','value')
			->from('countrylists')
			->where('approve','=','1')
			->order_by('position', 'ASC')
			->order_by('value', 'ASC')
			->execute();
		$countryarrays = $countries->as_array();
		
		foreach($countryarrays as $countryarray)
		{
			$countrylist[$countryarray['id']] = $countryarray['value'];
		}
		
		return $countrylist;
	}
	
  // Create List of User Roles and Role Key as Array
	public function user_privilege()
	{
		$userroles = DB::select('key','value')
			->from('userroles')
			->order_by('key', 'ASC')
			->execute();
		$rolearrays = $userroles->as_array();
		$rolelist[0]="Pilih";
		foreach($rolearrays as $rolearray)
		{
			$rolelist[$rolearray['key']] = $rolearray['value'];
		}
		
		return $rolelist;
	}


  // Create List of User's Nickname and ID as Array
	public function user_nama($role)
	{
		$nama = DB::select('id','nama')
			->from('users')
			->order_by('nama', 'ASC');
			if(!empty($role))
			{
				$nama = $nama->where('userroles_key','=',$role);
			}
			$nama = $nama->execute();
		$usernamaarrays = $nama->as_array();
		
		$userlist[0] = 'All';
		
		foreach($usernamaarrays as $usernamaarray)
		{
			$userlist[$usernamaarray['id']] = $usernamaarray['nama'];
		}
		
		return $userlist;
	}
	
  // Create List of Array From Tables
  // Give $empty_strings = NULL if don't want to set the Empty Strings
	public function default_array($table, $empty_strings='Choose Selection')
	{
		$dataarray = DB::select('id','value')
			->from($table)
			->execute();
		$defaultarrays = $dataarray->as_array();
		
		if(!is_null($empty_strings)) {
			$arraylist[0] = $empty_strings;
		}
		
		foreach($defaultarrays as $defaultarray)
		{
			$arraylist[$defaultarray['id']] = $defaultarray['value'];
		}
		
		return $arraylist;
	}

	public function default_array2($table, $empty_strings='Choose Selection')
	{
		$dataarray = DB::select()
			->from($table)
			->execute();
		$defaultarrays = $dataarray->as_array();
		
		if(!is_null($empty_strings)) {
			$arraylist[0] = $empty_strings;
		}
		
		foreach($defaultarrays as $defaultarray)
		{
			$arraylist[$defaultarray['id']] = $defaultarray['id'];
		}
		
		return $arraylist;
	}

	public function print_array($table, $id)
	{
		$dataarray = DB::select()
			->from($table)
			->where('id','=',$id)
			->execute();
		$defaultarrays = $dataarray->as_array();
		if(!empty($defaultarrays))
		{
			return $defaultarrays;
		} else {
			return NULL;
		}
		
		
	}

	public function print_array_child($table, $id_master)
	{
		$dataarray = DB::select()
			->from($table)
			->where('id_master','=',$id_master)
			->execute();
		$defaultarrays = $dataarray->as_array();
		if(!empty($defaultarrays))
		{
			return $defaultarrays;
		} else {
			return NULL;
		}
		
		
	}

  // Create List of Array From Tables and Field For Autocomplete Input Box
	public function autocomplete_array($table, $field)
	{
		$returndata = '';

		$autocompletelists = DB::select($field)
			->from($table)
			->order_by($field, 'ASC')
			->execute()
			->as_array();

		foreach($autocompletelists as $autocompletelist)
		{
			$string = $autocompletelist[$field];
			$newstr = preg_replace('/["]/', ' ', $string);

			$thedatas[] = '"'.$newstr.'"';
		}
		$datacount = count($thedatas);

		for ($loop_data = 0; $loop_data < $datacount; $loop_data++) {
		    $returndata .= $thedatas[$loop_data];
		    if($loop_data != $datacount-1)
		    {
		    	$returndata .= ',';
		    }
		}
		
		return $returndata;
	}
	
	public function autocomplete_supllier()
	{
		$returndata = '';

		$autocompletelists = DB::select('fullname')
			->from('users')
			->order_by('fullname', 'ASC')
			->where('userroles_key','=','950')
			->execute()
			->as_array();

		foreach($autocompletelists as $autocompletelist)
		{
			$string = $autocompletelist['fullname'];
			$newstr = preg_replace('/["]/', ' ', $string);

			$thedatas[] = '"'.$newstr.'"';
		}
		$datacount = count($thedatas);

		for ($loop_data = 0; $loop_data < $datacount; $loop_data++) {
		    $returndata .= $thedatas[$loop_data];
		    if($loop_data != $datacount-1)
		    {
		    	$returndata .= ',';
		    }
		}
		
		return $returndata;
	}

	public function autocomplete_prodbrands()
	{
		$returndata = '';

		$autocompletelists = DB::select('value')
			->from('prodbrands')
			->order_by('value', 'ASC')
			->where('approve','=','1')
			->execute()
			->as_array();

		foreach($autocompletelists as $autocompletelist)
		{
			$string = $autocompletelist['value'];
			$newstr = preg_replace('/["]/', ' ', $string);

			$thedatas[] = '"'.$newstr.'"';
		}
		$datacount = count($thedatas);

		for ($loop_data = 0; $loop_data < $datacount; $loop_data++) {
		    $returndata .= $thedatas[$loop_data];
		    if($loop_data != $datacount-1)
		    {
		    	$returndata .= ',';
		    }
		}
		
		return $returndata;
	}

	public function autocomplete_prodcategories()
	{
		$returndata = '';

		$autocompletelists = DB::select('value')
			->from('prodcategories')
			->order_by('value', 'ASC')
			->where('approve','=','1')
			->execute()
			->as_array();

		foreach($autocompletelists as $autocompletelist)
		{
			$string = $autocompletelist['value'];
			$newstr = preg_replace('/["]/', ' ', $string);

			$thedatas[] = '"'.$newstr.'"';
		}
		$datacount = count($thedatas);

		for ($loop_data = 0; $loop_data < $datacount; $loop_data++) {
		    $returndata .= $thedatas[$loop_data];
		    if($loop_data != $datacount-1)
		    {
		    	$returndata .= ',';
		    }
		}
		
		return $returndata;
	}

	public function autocomplete_invoiceOP()
	{
		$returndata = '';

		$autocompletelists = DB::select('invoicenumbers')
			->from('accinvoices')
			->order_by('invoicenumbers', 'ASC')
			->where('invoice_code','=','OP')
			->execute()
			->as_array();
		if(!empty($autocompletelists))
		{
		foreach($autocompletelists as $autocompletelist)
		{
			$string = $autocompletelist['invoicenumbers'];
			$newstr = preg_replace('/["]/', ' ', $string);

			$thedatas[] = '"'.'OP'.$newstr.'"';
		}
		$datacount = count($thedatas);

		for ($loop_data = 0; $loop_data < $datacount; $loop_data++) {
		    $returndata .= $thedatas[$loop_data];
		    if($loop_data != $datacount-1)
		    {
		    	$returndata .= ',';
		    }
		}
		} else {
			$returndata = NULL;
		}
		return $returndata;
	}

	public function autocomplete_products_array()
	{
		$returndata = '';

		$autocompletelists = DB::select('code','name')
			->from('products')
			->order_by('name', 'ASC')
			->execute()
			->as_array();

		foreach($autocompletelists as $autocompletelist)
		{
			$string = $autocompletelist['name'];
			$newstr = preg_replace('/["]/', ' ', $string);

			$thedatas[] = '"'.$newstr.'"';
		}
		$datacount = count($thedatas);

		for ($loop_data = 0; $loop_data < $datacount; $loop_data++) {
		    $returndata .= $thedatas[$loop_data];
		    if($loop_data != $datacount-1)
		    {
		    	$returndata .= ',';
		    }
		}
		
		return $returndata;
	}

	public function autocomplete_barcode_pcs_array()
	{
		$returndata = '';

		$autocompletelists = DB::select('code','barcode_pcs','name')
			->from('products')
			->order_by('barcode_pcs', 'ASC')
			->execute()
			->as_array();

		foreach($autocompletelists as $autocompletelist)
		{
			$string = $autocompletelist['name'];
			$newstr = preg_replace('/["]/', ' ', $string);

			$thedatas[] = '"'.$newstr.'#'.$autocompletelist['barcode_pcs'].'"';
		}
		$datacount = count($thedatas);

		for ($loop_data = 0; $loop_data < $datacount; $loop_data++) {
		    $returndata .= $thedatas[$loop_data];
		    if($loop_data != $datacount-1)
		    {
		    	$returndata .= ',';
		    }
		}
		
		return $returndata;
	}

	public function autocomplete_invoicePO()
	{
		$returndata = '';

		$autocompletelists = DB::select('invoicenumbers')
			->from('accinvoices')
			->order_by('invoicenumbers', 'ASC')
			->where('invoice_code','=','PO')
			->execute()
			->as_array();

		if(!empty($autocompletelists))
		{
			foreach($autocompletelists as $autocompletelist)
			{
				$string = $autocompletelist['invoicenumbers'];
				$newstr = preg_replace('/["]/', ' ', $string);

				$thedatas[] = '"'.'PO'.$newstr.'"';
			}
			$datacount = count($thedatas);

			for ($loop_data = 0; $loop_data < $datacount; $loop_data++) {
			    $returndata .= $thedatas[$loop_data];
			    if($loop_data != $datacount-1)
			    {
			    	$returndata .= ',';
			    }
			}
		} else {
			$returndata = NULL;
		}
		
		
		return $returndata;
	}

	// ----------------------------------------------------------------------------------------------------------array satuan

	public function satuan_array($default)
	{
		$dataarray = DB::select('id','value')
			->from('satuan')
			->order_by('id', 'ASC')
			->execute();
		$arrays = $dataarray->as_array();
		$rolelist[0]=$default;
		foreach($arrays as $rolearray)
		{
			$rolelist[$rolearray['id']] = $rolearray['value'];
		}
		
		return $rolelist;
	}

	// ---------------------------------------------------------------------------------------------------------end array satuan

	public function codemanagement_array($table, $empty_strings='Choose Selection')
	{
		$dataarray = DB::select('id','value')
			->from($table)
			->execute();
		$defaultarrays = $dataarray->as_array();
		
		if(!is_null($empty_strings)) {
			$arraylist[0] = $empty_strings;
		}
		
		foreach($defaultarrays as $defaultarray)
		{
			$arraylist[$defaultarray['id']] = $defaultarray['value'];
		}
		
		return $arraylist;
	}

	public function show_id_by_acccodes($acccodes)
	{
		$show = DB::select('id')
			->from('acccodes')
			->where('acccodegroups_keys','=',substr($acccodes,0,1))
			->where('acccodemasters_key','=',substr($acccodes,1,1))
			->where('keys','=',substr($acccodes,2,3))
			->limit(1)
			->execute()->as_array();
		
		if(!empty($show)) {
			$return = $show[0]['id'];
		} else {
			$return = NULL;
		}
		
		return $return;
	}

	public function tes()
	{
		$show = DB::select()
			->from('users')
			->execute()->as_array();
		
		
		return $show;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}